{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pickle\n",
    "from keras import datasets\n",
    "import h5py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def unpickle(file):\n",
    "    with open(file, 'rb') as fo:\n",
    "        res = pickle.load(fo, encoding='bytes')\n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "meta = unpickle(\"cifar100_meta\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys([b'fine_label_names', b'coarse_label_names'])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "meta.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "coarse_label_names = [s.decode('utf-8') for s in meta[b'coarse_label_names']]\n",
    "max([len(s) for s in coarse_label_names])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fine_label_names = [s.decode('utf-8') for s in meta[b'fine_label_names']]\n",
    "max([len(s) for s in fine_label_names])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([b'aquatic_mammals', b'fish', b'flowers', b'food_containers',\n",
       "       b'fruit_and_vegetables', b'household_electrical_devices',\n",
       "       b'household_furniture', b'insects', b'large_carnivores',\n",
       "       b'large_man-made_outdoor_things', b'large_natural_outdoor_scenes',\n",
       "       b'large_omnivores_and_herbivores', b'medium_mammals',\n",
       "       b'non-insect_invertebrates', b'people', b'reptiles',\n",
       "       b'small_mammals', b'trees', b'vehicles_1', b'vehicles_2'], \n",
       "      dtype='|S30')"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array(coarse_label_names, dtype='S30')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading data from http://www.cs.toronto.edu/~kriz/cifar-100-python.tar.gz\n",
      "168755200/169001437 [============================>.] - ETA: 0s"
     ]
    }
   ],
   "source": [
    "(x_train, y_train), (x_test, y_test) = datasets.cifar100.load_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = h5py.File(\"cifar100.h5\")\n",
    "f.create_dataset('x_train', data=x_train)\n",
    "f.create_dataset('y_train', data=y_train)\n",
    "f.create_dataset('x_test', data=x_test)\n",
    "f.create_dataset('y_test', data=y_test)\n",
    "f.create_dataset('coarse_label_names', data=np.array(coarse_label_names, dtype='S30'))\n",
    "f.create_dataset('fine_label_names', data=np.array(fine_label_names, dtype='S13'))\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
